package com.zsl.casuallyheart.login;

import cn.bmob.im.BmobUserManager;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.update.BmobUpdateAgent;

import com.zsl.casuallyheart.AppBaseActivity;
import com.zsl.casuallyheart.R;
import com.zsl.casuallyheart.bean.User;
import com.zsl.casuallyheart.main.Main;
import com.zsl.casuallyheart.reg.Register;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 登录
 * 
 * @author zsl
 * @date 2014-10-2下午5:37:17
 * @version v1.0
 */
public class Login extends AppBaseActivity {
	/**
	 * 控件
	 */
	private EditText login_et_username;
	private EditText login_et_pwd;
	private TextView login_bt_reg;
	private TextView login_bt_login;

	/**
	 * 数据
	 */
	private BmobUserManager bmobUserManager;
	String username, pwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		bmobUserManager=BmobUserManager.getInstance(this);
		// app检查更新
		appUpdate(this);
		initView();

	}
	/**
	 * 初始化控件
	 */
	private void initView() {
		login_et_username = (EditText) findViewById(R.id.login_et_username);
		login_et_pwd = (EditText) findViewById(R.id.login_et_pwd);
		login_bt_login = (TextView) findViewById(R.id.login_bt_login);
		login_bt_reg = (TextView) findViewById(R.id.login_bt_reg);
		login_bt_reg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Login.this, Register.class);
				startActivity(intent);
			}
		});

		// 登录
		login_bt_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				username = login_et_username.getText().toString();
				pwd = login_et_pwd.getText().toString();
				if (TextUtils.isEmpty(username)) {
					Toast.makeText(Login.this, "用户名不能为空", Toast.LENGTH_SHORT)
							.show();
				} else if (TextUtils.isEmpty(pwd)) {
					Toast.makeText(Login.this, "密码不能为空", Toast.LENGTH_SHORT)
							.show();
				} else {
					login(Login.this, username, pwd);
				}
			}
		});
	}

	/**
	 * 初始化数据
	 */
	private void initData() {

	}

	/**
	 * 登录
	 * 
	 * @param context
	 */
	public void login(final Context context, final String uname,
			final String password) {
		bmobUserManager.login(uname, password, new SaveListener() {
			@Override
			public void onSuccess() {
				// TODO Auto-generated method stub
				Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(context, Main.class);
				startActivity(intent);
				Login.this.finish();
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "用户名密码错误", Toast.LENGTH_SHORT).show();
			}
		});

	}

	/**
	 * app更新
	 */
	private void appUpdate(Context context) {
		// 应用更新初始化
		BmobUpdateAgent.initAppVersion(context);
		// 一般更新
		BmobUpdateAgent.update(context);
		// 手动更新
		// BmobUpdateAgent.forceUpdate(context);
		// 静默下载更新
		// BmobUpdateAgent.silentUpdate(context);
		// 是否只在wifi环境下提示
		BmobUpdateAgent.setUpdateOnlyWifi(false);
		// 检查前两部是否完成
		// BmobUpdateAgent.setUpdateCheckConfig(false);
	}

}
